/**
 * Copyright 2013-2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */	  

package com.cos.controller.app.appuser.workplan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cos.controller.base.BaseController;
import com.cos.service.system.appuser.workplan.WorkPlanCommenService;
import com.cos.util.AppUtil;
import com.cos.util.PageData;
import com.cos.util.ResponseUtil;
import com.cos.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 工作分工接口
 * <p/>
 * 
 * @author <a href="15083378620@163.com">LXJ</a>
 * @version  Date: 2016年3月25日 上午10:40:34
 * @serial 1.0
 * @since 2016年3月25日 上午10:40:34
 */

@Controller
@RequestMapping(value ="/appuser")
public class WorkPlanCommenController extends BaseController {
	
	
	@Resource(name="workPlanCommenService")
	private WorkPlanCommenService workPlanCommenService;
	
	
	/**
	 * 获取工作分工数据
	 * @param response  可以是用户ID 可以是部门ID  用户ID获取的就是自己发的工作分工
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getWorkPlanByUID")
	@ResponseBody
	public Object getWorkPlanByUID(HttpServletResponse response) throws Exception{
		logBefore(logger,"获取工作分工");
		PageData pd = new PageData();
		JSONObject workplan = new JSONObject();
		pd = this.getPageData();
		String result = "00";
			try {
				if(AppUtil.checkParam("getWorkPlanByUID", pd)){//检查参数  如果参数不是U_ID 就返回03
					List<PageData> workplanList = new ArrayList<PageData>();
					pd.put("TIME", StringUtil.formatLike((String) pd.get("TIME")));
					workplanList = workPlanCommenService.getWorkPlan(pd);
					JSONArray rows = JSONArray.fromObject(workplanList);
					result = (null == workplanList) ? "02" : "01";
					workplan.put("rows", rows);
				}else{
					result = "03";
				}
			}catch (Exception e) {
				logger.error(e.toString(),e);
			}finally{
				workplan.put("result",result);
				logAfter(logger);
			}
			ResponseUtil.write(response, workplan);
			
		return null;
	}
	
	
	/**
	 * 获取工作分工数据
	 * @param response  可以是用户ID 可以是部门ID  用户ID获取的就是自己发的工作分工
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getWorkPlanByDID")
	@ResponseBody
	public Object getWorkPlanByDID(HttpServletResponse response) throws Exception{
		logBefore(logger,"获取工作分工");
		PageData pd = new PageData();
		JSONObject workplan = new JSONObject();
		pd = this.getPageData();
		String result = "00";
			try {
				if(AppUtil.checkParam("getWordPlanByDID", pd)){//检查参数  如果参数不是D_ID 就返回03
					List<PageData> workplanList = new ArrayList<PageData>();
					pd.put("TIME", StringUtil.formatLike((String) pd.get("TIME")));
					workplanList = workPlanCommenService.getWorkPlan(pd);
					JSONArray rows = JSONArray.fromObject(workplanList);
					result = (null == workplanList) ? "02" : "01";
					workplan.put("rows", rows);
				}else{
					result = "03";
				}
			}catch (Exception e) {
				// TODO: handle exception
				logger.error(e.toString(),e);
			}finally{
				workplan.put("result",result);
				logAfter(logger);
			}
			ResponseUtil.write(response, workplan);
			return null;
	}
	
	/**
	 * 获取工作分工详情
	 * @param response  工作分工的ID P_ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getWordPlanDetail")
	@ResponseBody
	public Object getWordPlanDetail(HttpServletResponse response) throws Exception{
		logBefore(logger, "获取工作分工详情");
		Map<String,Object> map = new HashMap<String,Object>();
		JSONObject workAdviceDetail = new JSONObject();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getWorkAdviceID", pd)){//检查参数 
				PageData wordAdvice = new PageData();
				wordAdvice = workPlanCommenService.getWordPlanDetail(pd);
				JSONArray rows = JSONArray.fromObject(wordAdvice);
				result = (null == wordAdvice) ?  "02" :  "01";
				workAdviceDetail.put("rows", rows);
			}else{
				result = "03";
			}
		}catch(Exception e){
			logger.error(e.toString(), e);
		}finally{
			workAdviceDetail.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, workAdviceDetail);
		return null;
	}
	
	/**
	 * 获取执行组
	 * @param response P_ID工作分工ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getExecute")
	@ResponseBody
	public Object getExecute(HttpServletResponse response) throws Exception{
		logBefore(logger, "获取工作分工详情");
		JSONObject workAdviceDetail = new JSONObject();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getWorkAdviceID", pd)){//检查参数 
				List<PageData> workplanList = new ArrayList<PageData>();
				workplanList = workPlanCommenService.getExecute(pd);
				JSONArray rows = JSONArray.fromObject(workplanList);
				result = (null == workplanList) ?  "02" :  "01";
				workAdviceDetail.put("rows", rows);
			}else{
				result = "03";
			}
		}catch(Exception e){
			logger.error(e.toString(), e);
		}finally{
			workAdviceDetail.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, workAdviceDetail);
		return null;
	}
	
	/**
	 * 获取工作痕迹列表通过部门ID
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getWorkMemoryByDID")
	@ResponseBody
	public Object getWorkMemoryByDID(HttpServletResponse response) throws Exception{
		logBefore(logger, "获取工作分工详情");
		JSONObject workAdviceDetail = new JSONObject();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getWordAdviceByDID", pd)){//检查参数 
				List<PageData> workplanList = new ArrayList<PageData>();
				workplanList = workPlanCommenService.getWorkMemory(pd);
				JSONArray rows = JSONArray.fromObject(workplanList);
				result = (null == workplanList) ?  "02" :  "01";
				workAdviceDetail.put("rows", rows);
			}else{
				result = "03";
			}
		}catch(Exception e){
			logger.error(e.toString(), e);
		}finally{
			workAdviceDetail.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, workAdviceDetail);
		return null;
	}
	
	
	/**
	 * 获取工作痕迹列表通过用户ID
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getWorkMemoryByUID")
	@ResponseBody
	public Object getWorkMemoryByUID(HttpServletResponse response) throws Exception{
		logBefore(logger, "获取工作分工详情");
		JSONObject workAdviceDetail = new JSONObject();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getGroup", pd)){//检查参数 
				List<PageData> workplanList = new ArrayList<PageData>();
				workplanList = workPlanCommenService.getWorkMemory(pd);
				JSONArray rows = JSONArray.fromObject(workplanList);
				result = (null == workplanList) ?  "02" :  "01";
				workAdviceDetail.put("rows", rows);
			}else{
				result = "03";
			}
		}catch(Exception e){
			logger.error(e.toString(), e);
		}finally{
			workAdviceDetail.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, workAdviceDetail);
		return null;
	}
	
	
	/**
	 * 获取工作痕迹详情
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getWorkMemoryDetail")
	@ResponseBody
	public Object getWorkMemoryDetail(HttpServletResponse response) throws Exception{
		logBefore(logger, "获取工作痕迹详情");
		JSONObject workAdviceDetail = new JSONObject();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getWorkAdviceID", pd)){//检查参数 
				PageData workplanList = new PageData();
				workplanList = workPlanCommenService.getWorkMemoryDetail(pd);
				JSONArray rows = JSONArray.fromObject(workplanList);
				result = (null == workplanList) ?  "02" :  "01";
				workAdviceDetail.put("rows", rows);
			}else{
				result = "03";
			}
		}catch(Exception e){
			logger.error(e.toString(), e);
		}finally{
			workAdviceDetail.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, workAdviceDetail);
		return null;
	}
	
	/**
	 * 工作痕迹评论 
	 * @param response  P_ID  TYPE_ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getPlanComment")
	@ResponseBody
	public Object getPlanComment(HttpServletResponse response) throws Exception{
		logBefore(logger, "获取主管通知列表");
		JSONObject workAdviceDetail = new JSONObject();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getWorkAdviceID", pd)){//检查参数 
				List<PageData> workplanList = new ArrayList<PageData>();
				workplanList = workPlanCommenService.getPlanComment(pd);
				JSONArray rows = JSONArray.fromObject(workplanList);
				result = (null == workplanList) ?  "02" :  "01";
				workAdviceDetail.put("rows", rows);
			}else{
				result = "03";
			}
		}catch(Exception e){
			logger.error(e.toString(), e);
		}finally{
			workAdviceDetail.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, workAdviceDetail);
		return null;
	}
	
	/**
	 * 获取部门下的用户 
	 * @param response  C_ID D_ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getUserByDID")
	@ResponseBody
	public Object getUserByDID(HttpServletResponse response) throws Exception{
		logBefore(logger, "通过部门ID获取用户");
		JSONObject workAdviceDetail = new JSONObject();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getWordAdviceByDID", pd)){//检查参数 
				List<PageData> workplanList = new ArrayList<PageData>();
				workplanList = workPlanCommenService.getUserByDID(pd);
				JSONArray rows = JSONArray.fromObject(workplanList);
				result = (null == workplanList) ?  "02" :  "01";
				workAdviceDetail.put("rows", rows);
			}else{
				result = "03";
			}
		}catch(Exception e){
			logger.error(e.toString(), e);
		}finally{
			workAdviceDetail.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, workAdviceDetail);
		return null;
	}
	
	/**
	 * 发布工作分工
	 * @param response    CONTENT 内容  
	 * 					  STAND   标准
	 * 					  MONEY   钱
	 * 					  START_TIME时间
	 * 					  CREATE_TIME创建时间
	 * 					  U_ID  用户ID
	 * 					  C_ID
	 * 					  D_ID	
	 * 					  UIDS   执行组中用户ID拼接的字符串组合	
	 * 					  TIDS   用户的身份组合				 				
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping(value="/saveWordPlan")
	@ResponseBody
	public Object saveWordPLAN(HttpServletResponse response) throws Exception{
		logBefore(logger, "获取工作分工");
		JSONObject workAdvice = new JSONObject();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("saveWordPLAN", pd)){
				String []UIDS=pd.getString("UIDS").split(",");
				String []TIDS=pd.getString("TIDS").split(",");
				
				pd.put("ID", this.get32UUID());
				pd.put("SUPPORT_NUM","0");
				pd.put("TRAMPLE_NUM","0");
				workPlanCommenService.saveWordPlan(pd);
				String P_ID = pd.getString("ID");
				for(int i=0;i<UIDS.length;i++){
					pd.put("E_ID", this.get32UUID());
					pd.put("UIDS", UIDS[i]);
					pd.put("TIDS", TIDS[i]);
					pd.put("P_ID", P_ID);
					workPlanCommenService.saveExecuteGroup(pd);
				}
				result =  "02";
			}else{ 
				result = "03";
			}
		}catch(Exception e){
			logger.error(e.toString(), e);
		}finally{
			workAdvice.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, workAdvice);
		return null;
	}
	
	/**
	 * 发布主管通知
	 * @param response    CONTENT 内容  
	 * 					  CREATE_TIME创建时间
	 * 					  U_ID  用户ID
	 * 					  P_ID
	 * 					  D_ID	
	 * 					  				 				
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping(value="/saveQaRecommend")
	@ResponseBody
	public Object saveQaRecommend(HttpServletResponse response) throws Exception{
		logBefore(logger, "获取工作分工");
		JSONObject workAdvice = new JSONObject();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("saveQaRecommend", pd)){
				
				pd.put("ID", this.get32UUID());
				pd.put("TYPE_ID", 1);
				workPlanCommenService.saveQaRecommend(pd);
				result =  "02";
			}else{ 
				result = "03";
			}
		}catch(Exception e){
			logger.error(e.toString(), e);
		}finally{
			workAdvice.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, workAdvice);
		return null;
	}
	
	
	/**
	 * 发布主管通知
	 * @param response    CONTENT 内容  
	 * 					  CREATE_TIME创建时间
	 * 					  U_ID  用户ID
	 * 					  P_ID
	 * 					  D_ID	
	 * 					  				 				
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping(value="/savePlanRecommend")
	@ResponseBody
	public Object savePlanRecommend(HttpServletResponse response) throws Exception{
		logBefore(logger, "获取工作分工");
		JSONObject workAdvice = new JSONObject();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("saveQaRecommend", pd)){
				
				pd.put("ID", this.get32UUID());
				pd.put("TYPE_ID", 2);
				workPlanCommenService.saveQaRecommend(pd);
				result =  "02";
			}else{ 
				result = "03";
			}
		}catch(Exception e){
			logger.error(e.toString(), e);
		}finally{
			workAdvice.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, workAdvice);
		return null;
	}
}
